package cn.erp.app.system.controller;

import java.util.ArrayList;
import java.util.List;

import cn.erp.app.service.BaseInfoService;
import cn.erp.app.service.ProduceService;
import cn.erp.app.util.EuiRender;
import cn.erp.common.model.SvcBaseinfo;
import cn.erp.common.model.SvcMaterielSubdivision;
import cn.erp.common.model.SvcProduceCategory;
import cn.erp.common.model.SystemMenu;

import com.jfinal.common.controller.BaseController;
import com.jfinal.ext.ret.ReqResult;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

@ControllerBind(controllerKey = "/system/produce", viewPath = "/system/produce")
public class ProduceController extends BaseController {
	public void index() {
		render("produce_index.html");
	}

	public void getdata() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		// Page<SvcProduceCategory> result = null;
		// try{
		// result = ProduceService.me.getAllWithPaginate(pageNumber,pageSize);
		// render(EuiRender.page(result));
		// return;
		// } catch(Exception ex) {
		// ex.printStackTrace();
		// }
		// renderJson(ReqResult.QUERY_DETAIL_FAILED);

		SvcProduceCategory item = getModel(SvcProduceCategory.class, "produce");
		String where = "from svc_produce_category where 1=1  ";
		List<Object> objs = new ArrayList<Object>();
		if (item.getProduceName() != null) {
			where += " and ProduceName = ? ";
			objs.add(item.getProduceName());
		}

		// System.out.println(objs.toArray());
		Page<SvcProduceCategory> result = SvcProduceCategory.dao.paginate(
				pageNumber, pageSize, "select * ", where, objs.toArray());
		render(EuiRender.page(result));
	}

	public void search() {
		int pageSize = getParaToInt("page", 1);
		int pageNumber = getParaToInt("rows", 10);
		String search = getPara("search");
		Page<SvcProduceCategory> result = null;
		try {
			result = ProduceService.me
					.getBySearch(pageNumber, pageSize, search);
			render(EuiRender.page(result));
			return;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		renderJson(ReqResult.QUERY_DETAIL_FAILED);
	}

	public void openadd() {
		long id= getParaToLong("id",0L);
		SvcProduceCategory pojo = new SvcProduceCategory();
		if(id!=0L) {
			pojo = SvcProduceCategory.dao.findById(id);
			
		}
		setAttr("pojo",pojo);
		render("produce_add.html");
	}
	
	public void saveorupdate() {
		SvcProduceCategory svcProduce = getModel(SvcProduceCategory.class, "");
		try {
			if (svcProduce.getId() != null && svcProduce.getId() != 0) {
				svcProduce.update();
			} else {
				svcProduce.save();
			}
			renderJson(ReqResult.ADD_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.ADD_ERROR);
	}

	public void delete() {
		String[] ids = getParaValues("ids");
		try {
			if (ids != null && !ids.equals("")) {
				for (String id : ids) {
					SvcProduceCategory.dao.deleteById(id);
				}
			}
			renderJson(ReqResult.DEL_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.DEL_ERROR);

	}

	public void edit() {
		long id = getParaToLong("id");
		try {
			SvcProduceCategory svcProduce = SvcProduceCategory.dao.findById(id);
			renderJson(svcProduce);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.ADD_ERROR);
	}
}
